
(in-package :gtcup)

(clsql:disable-sql-reader-syntax)
(clsql:enable-sql-reader-syntax)

(defparameter *names* ())
(defparameter *corridas* ())

(defmacro with-html (&body body)
  `(with-html-output-to-string (*standard-output* nil :prologue t)
     ,@body))

(defun resultado-page ()
  (let ((corrida-selecionada (hunchentoot:post-parameter "corrida-selecionada")))
    (setf *names* (clsql:select 'resultado :where [= [slot-value 'resultado 'corrida-id] 
				(if corrida-selecionada corrida-selecionada 1)] 
				:order-by '(([voltas] :desc) ([tempo-corrida] :asc))))
    (setf *corridas* (clsql:select 'corrida :refresh t))
    (hunchentoot:no-cache)
    (with-html
      (:html
       (:head (:title "GTCup Brasil"))
       (:body
	(:h1 "GTCup Brasil Resultados")
	(:form :method :post
	       (:select :name "corrida-selecionada" :onchange "submit()"
			(dolist (um-resultado *corridas*)
			  (htm
			   (if (equalp (id (car um-resultado)) (corrida-id (car (car *names*))))
			       (htm
				(:option 
				 :selected t
				 :value (id (car um-resultado))
				 (esc (nome (load-circuito-by-arquivo 
					     (nome (car um-resultado)))))
				 " em "
				 (esc (clsql:print-date (data (car um-resultado))))))
			       (htm
				(:option 
				 :selected nil
				 :value (id (car um-resultado))
				 (esc (nome (load-circuito-by-arquivo 
					     (nome (car um-resultado)))))
				 " em "
				 (esc (clsql:print-date (data (car um-resultado))))))))))
	       (:input
		:type "submit"
		:value "Ok"))
      (display-resultado *names*))))))


(defun display-resultado (resultado)
  (with-html-output (*standard-output*)
    (htm
  	(:p
	  (:h1 :align :center (format t "~a"
	               (nome (load-circuito-by-arquivo 
					     (nome (corrida (car (car resultado)))))))) 
	               
	  (:table :border 1 :cellspacing 0 :cellpadding 0 :align :center 
	    (:tr :bgcolor "gray" :style "font-style:italic"  
	      (:td "Posicao")
	      (:td "Piloto")
	      (:td "Voltas")
	      (:td "Melhor Volta")
	      (:td "Pontos"))
	    (dolist (um resultado) 
	      (htm
	       (:tr :bgcolor (if (oddp (posicao (car um))) "#6bcff7" "white")
	         (:td (format t "~a" (posicao (car um))))
	         (:td (format t "~a" (nome (piloto (car um)))))
	         (:td (format t "~a" (voltas (car um))))
	         (:td (format t "~a" (melhor-volta (car um))))
	         (:td (format t "~a" (load-pontuacao-by-campeonato-posicao 
	                              (campeonato (corrida (car um))) 
	                              (posicao (car um)))))))))))))

(defun classificacao-page ()
  (let ((um-campeonato (car (car (clsql:select 'campeonato :where [= [slot-value 'campeonato 'id] 3])))))
    (hunchentoot:no-cache)
    (with-html
      (:html
       (:head (:title "GTCup Brasil"))
       (:body
	(:h1 :align :center "GTCup Brasil Classificacao")
	(:h1 :align :center (format t "~a" (nome um-campeonato)))
	(:p
	 (:table :border 1 :cellspacing 0 :cellpadding 0 :align :center 
		   (:tr :bgcolor "gray" :style "font-style:italic"  
		   (:td "Posicao")
		   (:td "Piloto")
		   (:td "Pontos"))
	   (let ((posicao 1))
	     (dolist (linha (sort (list-piloto-pontuacao-by-campeonato um-campeonato) #'> :key #'car))
	       (htm
	        (:tr :bgcolor (if (oddp posicao) "#6bcff7" "white")
	          (:td :align :center (format t "~a" posicao))
	          (:td (format t "~a" (car (cdr linha))))
	          (:td (format t "~a" (car linha)))))
	       (setf posicao (+ 1 posicao))))))
         (:p
           (dolist (um-corrida (corridas um-campeonato))
             (display-resultado (load-resultados-by-corrida (id um-corrida))))))))))
                


(setq hunchentoot:*dispatch-table* 
      (list 
        (hunchentoot:create-prefix-dispatcher "/resultado" 'resultado-page)
        (hunchentoot:create-prefix-dispatcher "/classificacao" 'classificacao-page)))